/**
 * @author nullus
 */
function questionFormCanPost(){

    question_form = document.question_form;
    
    title = question_form.question_title.value;
    content = question_form.question_content.value;
    
    if ("" == title) {
        alert("問題名が空です。")
        return false;
    }
    else 
        if ("" == content) {
            alert("問題文が空です。")
            return false;
        }
    
    return true;
}

/**
 * @author nullus
 */
function showResult(){

    //あまり良いコードではないので、しっかりとしたリファレンスを読んで修正
    
    answer_form = document.answer_form;
    answers = answer_form.answer;

    if (answers.length) {
        checkCount = 0;
		checkNumber = 0;
        for (i = 0; i < answers.length; i++) {
			
            if (answers[i].checked) {
                checkCount++;
				checkNumber = i;
            }
		   
            if (checkCount > 1) {
                alert("解答は1つだけ選択してください");
                return false;
            }
        }
        
        if (checkCount == 0) {
            alert("解答をチェックしてください。");
            return false;
        }

		result = answers[checkNumber].value == "true" ? "正解" : "不正解";
	
		result_s = document.getElementById("result_s")
		if (result_s.childNodes.length == 0) {
			result_s.appendChild(document.createTextNode(result));			
		} else {
			result_s.firstChild.nodeValue = result;
		}

		//リダイレクトはさせない
	    return false;
    }
    else {// チェックボックスが1つの場合
        if (!answers.checked) {
            alaert("解答をチェックしてください。");
            return false;
        }
        
		result = answers.value == "true" ? "正解" : "不正解";
	
		result_s = document.getElementById("result_s")
		if (result_s.childNodes.length == 0) {
			result_s.appendChild(document.createTextNode(result));			
		} else {
			result_s.firstChild.nodeValue = result;
		}
		
		//リダイレクトはさせない
        return false;
    }
    
    //ここには来ない
    alaert("プログラムミス");
    return false;
}

function clearCheck(){
    //あまり良いコードではないので、しっかりとしたリファレンスを読んで修正
    
    answer_form = document.answer_form;
    
    answers = answer_form.answer;
    
    if (answer_form.answer.length) {
        for (i = 0; i < answers.length; i++) {
            answers[i].checked = false;
        }
        return true;
    }
    else {// チェックボックスが1つの場合
        if (!answers.checked) {
            answers.checked = false;
            return true;
        }
    }
    
    //ここには来ない
    alaert("プログラムミス");
    return false;
}

function removeSelection(index){
    question_form = document.question_form;
    selection_p = document.getElementById("selection" + index);
    question_form.removeChild(selection_p);
    
    selection_size = question_form.selection_size;
    selection_size.setAttribute("value", eval(selection_size.getAttribute("value")) - 1);
}

function addSelection(){
    question_form = document.question_form;
    
    question_form.appendChild(createSelectionElement());
    
    selection_size = question_form.selection_size;
    selection_size.setAttribute("value", eval(selection_size.getAttribute("value")) + 1);
    
}



function createSelectionElement(){
    //indexはゼロ始まりなので現在のsizeと一致する
    var index = document.question_form.selection_size.getAttribute("value");
    
    var selection = document.createElement("p");
    selection.setAttribute("id", ("selection" + index));
    
    var input_text = document.createElement("input");
    input_text.setAttribute("class", "selection_text");
    input_text.setAttribute("type", "text");
    input_text.setAttribute("name", "selection_text");
    input_text.setAttribute("value", "");
    input_text.setAttribute("maxlength", "1000");
    input_text.setAttribute("size", "20");
    selection.appendChild(input_text);
    
    //    var select_list = document.createElement("select");
    //    select_list.setAttribute("class", "selection_list");
    //    select_list.setAttribute("name", "selection_list");
    //    var list_on_change = "selection_text[" + index + "].value = this.value;"
    //    select_list.setAttribute("onchange", list_on_change);
    //    selection.appendChild(select_list);
    
    var input_correct = document.createElement("input");
    input_correct.setAttribute("class", "selection_is_correct");
    input_correct.setAttribute("type", "checkbox");
    input_correct.setAttribute("name", "is_correct");
    input_correct.setAttribute("value", index);
    selection.appendChild(input_correct);
    
    var span_is_correct_label = document.createElement("span");
    span_is_correct_label.appendChild(document.createTextNode("正答"));
    selection.appendChild(span_is_correct_label);
    
    var remove_a = document.createElement("a");
    remove_a.setAttribute("class", "remove_selection");
    remove_a.setAttribute("href", "javascript:void(0);");
    var remove_a_onclick = "removeSelection(" + index + ");"
    remove_a.setAttribute("onclick", remove_a_onclick);
    remove_a.appendChild(document.createTextNode("remove"));
    selection.appendChild(remove_a);
    
    return selection;
    
    //	selection.innerHTML = "
    //	   <p id='selection<%= i%>'><span>選択肢<%= i+1%></span>"
    //			<select name='selection_list' class='selection_list' onChange='selection_text[<%= i%>].value = this.value;'>
    //               <% if selections != nil
    //					selections.each { |s| %>
    //						<option><%= s.content%></option>
    //	               	<%}
    //                 end %>
    //            </select>
    //						<input type='checkbox' name='is_correct' value='<%= i%>'>正答
    //						<a href='javascript:void(0)' onclick='remove_selection(<%= i%>)' title='remove selection'>remove</a>
    //						</p>
    //						"
}


//function sendValueFromListToInput (i) {
//	select = document.selection_list[i];
//	text = document.selection_input[i];
//	alert(select.value);
//	alert(text.value);
//}
